/**
 * @author v.lugovsky
 * created on 16.12.2015
 */
(function () {
    'use strict';

    angular.module('BlurAdmin.pages.class')
        .controller('ClassSaveManageCtrl', ClassSaveManageCtrl);

    /** @ngInject */
    function ClassSaveManageCtrl($scope,ClassModel,id,AppUtils,toastr) {
        var selectLoadData = true;


        var vm = this;
        vm.disabled = undefined;
        vm.withSearchItem = {};
        vm.selectWithSearchItems = [];
        vm.withSearchItem.selected = "";
        $scope.saveClass = function(){
            ClassModel.updateClassDetails({
                id: id,
                teacherId: vm.withSearchItem.selected.id,
                className: vm.schoolClass.className,
                teacherName: vm.withSearchItem.selected.label,
                classInfomation: vm.schoolClass.classInfomation
            }).$promise.then(function (response) {toastr.success('保存成功！');})
        }

        vm.schoolClass = {};
        vm.init = function(){
            ClassModel.getClassTeacher({id:id}).$promise.then(function(response){
                vm.schoolClass = response.schoolClass;
                var obj = {};
                obj.id = vm.schoolClass.teacherId;
                obj.label = vm.schoolClass.teacherName;
                vm.selectWithSearchItems.push(obj);
                vm.withSearchItem.selected = vm.selectWithSearchItems[0];
                selectLoadData = false;
            })
        }
        vm.init();

        $scope.refreshAddresses = function(address) {
            if(!selectLoadData && !AppUtils.isUndefinedOrNull(address) && address != ""){
                ClassModel.queryTeacher({
                    memberName: address,
                    memberType: "1",
                }).$promise.then(function (response) {
                    var result = response.queryTeacher;
                    angular.forEach(result,function(data){
                        vm.selectWithSearchItems = [];
                        vm.withSearchItem.selected = "";
                        var obj = {};
                        obj.id = data.id;
                        obj.label = data.memberName;
                        vm.selectWithSearchItems.push(obj);
                    })
                });
            }
        };

        $scope.gobak = function(){AppUtils.goBack();}
    }

})();
